package org.example.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.example.domain.dto.GoodsMessage;
import org.example.domain.entity.UserAddr;
import org.example.domain.entity.UserName;
import org.example.domain.model.Result;
import org.example.domain.model.ShoppingCartAllInfo;
import org.example.domain.model.TableData;
import org.example.service.IOrderMessageService;
import org.example.service.IShoppingCartAllInfoService;
import org.example.service.IUserAddrService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/*
生成订单号

*/
@RestController
@RequestMapping("/orderNum")
//@CrossOrigin("*")
public class OrderNumberController {

    @Resource
    private IUserAddrService iUserAddrService;

    @Resource
    private IShoppingCartAllInfoService iShoppingCartAllInfoService;

    @Resource
    private IOrderMessageService iOrderMessageService;

//转化json字符串为dto层对象
//    @RequestMapping(value = "/goodsMessage", method = RequestMethod.POST, consumes = "application/json")
    @PostMapping("/goodsMessage")
    public Result goodsMessage(@Param("userId")Integer userId,@Param("shopId") String shopId){
        System.out.println("userID+++++++"+userId);
        System.out.println("shopId++++++++"+shopId);
        GoodsMessage goodsMessage = new GoodsMessage();
        goodsMessage.setUserId(userId);
        goodsMessage.setShopId(shopId);
//        System.out.println("11111111111"+goodsMessage);
        if(goodsMessage!=null){
////            System.out.println(goodsMessage);
////            JSONObject jsonObject = JSONObject.parseObject(goodsMessage);
////            JSONObject user = jsonObject.getJSONObject("goodsMessage");
////            GoodsMessage goodsMessage1 = user.toJavaObject(GoodsMessage.class);
////            System.out.println("+++SDASD_+ASF_+SF_+SF_+ASf");
//            System.out.println(goodsMessage.getShopId());
            String s = iOrderMessageService.orderList(goodsMessage);
            System.out.println(s);
            return Result.buildSuccess(s);
        }
        return null;
    }
    //获取结算页所有信息 返回页面进行渲染  shop表
    @PostMapping("/add")
    public void add(UserAddr site){
        System.out.println("add");
        System.out.println(site);
        iUserAddrService.addAddr(site);
    }

    //查id
    @PostMapping("/id")
    public Result userId(String userName){
        System.out.println("id");
        System.out.println("dfghjk;asfashfnalgiolajldsvchujdasdf");
        Integer userId = iOrderMessageService.queryId(userName);
        return Result.buildSuccess(userId);
    }

    @PostMapping("/all")
    public TableData<ShoppingCartAllInfo> allMessage(@Param("userId") Integer userId,@Param("shoppingId")String shoppingId){
        System.out.println("========");
        System.out.println(shoppingId);
        List<ShoppingCartAllInfo> shoppingCartAllInfo = iShoppingCartAllInfoService.selectAllShoppingCartToShoppingId(shoppingId);
        TableData<ShoppingCartAllInfo> shoppingCartAllInfoTableData = TableData.buildSuccess(shoppingCartAllInfo.size(), shoppingCartAllInfo);
        System.out.println(userId);
        return shoppingCartAllInfoTableData;
    }


    @PostMapping("/select")
    public TableData<UserAddr> selectAddr(Integer userId){
        System.out.println("select");
        System.out.println(userId);
        List<UserAddr> userAddrs = iUserAddrService.selectById(userId);
//        return Result.buildSuccess(userAddrs);
        TableData<UserAddr> tTableData = TableData.buildSuccess(userAddrs.size(), userAddrs);
        return tTableData;
    }

    //删除地址
    @PostMapping("/rmAddr")
    public Result mrAddr(Integer addrId){
        System.out.println("sdgaklshfaskdjhfaskjdfhasjdhfushdfjshddfjkasdhfhasldf==="+addrId);
        Integer integer = iUserAddrService.deleteById(addrId);
        if (integer==1){
            return Result.buildSuccess("success");
        }
        else {
            return Result.buildFail("fail");
        }
    }


    @PostMapping("/selectOne")
    public Result selectID(Integer userId){
        System.out.println("selectOne");
        System.out.println(userId);
        UserAddr user = iUserAddrService.selectByOneId(userId);
        return Result.buildSuccess(user);
    }


    //获取购物车传过来的数据  类型  String
    @PostMapping("/shop")
    public void shopOrderMessage(String shopId,Integer userId){
        System.out.println(userId);
        System.out.println(shopId);
    }
    @PostMapping("/del")
    public void delOrderId(String orderId){
        iOrderMessageService.delOrderId(orderId);
    }
}
